<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<title>title</title>
	<link rel="stylesheet" type="text/css" href="../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../css/aui.css" />
</head>

<body>

	<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
		<a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
			<span class="aui-iconfont aui-icon-left"></span>
		</a>
		<div class="aui-title" id="title">
			检测数据录入
		</div>
	</header>
	<div class="aui-content" style="margin-top:50px;">

		<div class="aui-form">
			<div class="aui-input-row">
				<span class="aui-input-addon" style="width: 20%;text-align: left">检测单号</span>
				<input type="text" class='aui-input' placeholder="选择检测单" style="width: 80%;" id="chkBillCode"
					value="" />
				<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectChkBill()"></i>
			</div>
			<div class="aui-input-row" style="display:none;">
				<span class="aui-input-addon" style="width: 20%;text-align: left">机台</span>
				<input type="text" class='aui-input' placeholder="选择机台" style="width: 80%;" id="workCellCodeNew"
					value="" />
				<i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCellNew()"></i>
			</div>
			<div class="aui-input-row">
				<span class="aui-input-addon" style="width: 20%;text-align: left">数量</span>
				<input type="number" class='aui-input' placeholder="" style="width: 80%;" id="detailNum" value="1" />
				<i class="aui-input-addon aui-iconfont "></i>
			</div>
			<div class="aui-input-row">
				<span class="aui-input-addon" style="width: 20%;text-align: left">序列号</span>
				<input type="text" class='aui-input' placeholder="" style="width: 80%;" id="serialNo" value=""
					onclick="getIdSelector();" />
				<i class="aui-input-addon aui-iconfont "></i>
			</div>
		</div>
		<br />
		<div class="aui-content aui-content-padded " style="text-align:center;margin-bottom: 5%;">
			<p>
			<div class="aui-btn aui-btn-info aui-btn-block" onclick="doInput()" style="width: 100%;height: 100%;">
				创建
			</div>
			</p>
		</div>
	</div>

	<div id="citemList" class="aui-content">
		<div class="aui-form" id="citemForm">

		</div>
	</div>

	<div class="aui-content">
		<div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
			<p>
			<div class="aui-btn aui-btn-info aui-btn-block" onclick="goInput()" style="width: 100%;height: 100%;">
				进入检测单关闭界面
			</div>
			</p>
		</div>
	</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
	apiready = function () {

		chkBillId = api.pageParam.billId;
		chkBillCode = api.pageParam.billCode;
		$("#chkBillCode").attr("value", api.pageParam.billCode);
	};


	var chkBillId = "";
	var chkBillCode = "";

	var retDetailId = "";

	var detailNum = "";

	var idSelector = "";
	// 获取光标所在组件的id
	function getIdSelector() {
		var e = event || window.event;
		var el = e.srcElement || e.target;
		idSelector = el.id;

		$("#" + idSelector).focus();
		$("#" + idSelector).select();
	}

	//检测单关闭界面
	function goInput() {
		// api.closeWin();
		api.openWin({
			name: 'qcmChkBillClose',
			url: 'qcmChkBillClose.html',
			pageParam: {
				chkBillCode: chkBillCode,
				name: "qcChkBillDetailInputFHJ",
			}
		});
	}

	//检测单选择后回调函数
	function setChkBill(id, code, mrlCode, mrlName, opCode, opName, chkTypeName) {
		chkBillId = id;
		chkBillCode = code;
		$("#chkBillCode").attr("value", code);
	}

	function goSelectChkBill() {
		api.openWin({
			name: 'qcmSelectChkBill',
			url: 'qcmSelectChkBill.html',
			pageParam: {
				processState: "0", //审核状态：0未处理，3处理中，7已处理，8已确认，9已取消
				chkBillType: "5",
				name: "qcChkBillDetailInputFHJ",
			}
		});
	}

	function closeMenu() {
		var moduleMenuView = api.require('moduleMenuView');
		var result = moduleMenuView.getSelectContent();
		alert(JSON.stringify(result));

		if (result.cell.length > 0) {
			moduleMenuView.close();
		}
	}

	//创建检测单明细
	function doInput() {
		var un = localStorage.getItem('un');
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/createUqcmChkBillDetailByChkBill";

		if (chkBillId == "") {
			api.alert({
				title: '提示',
				msg: '请选择检测单',
			}, function (ret, err) {

			});
			return;
		}

		detailNum = $("#detailNum").val();

		if (detailNum == "") {
			api.alert({
				title: '提示',
				msg: '请填写数量',
			}, function (ret, err) {

			});
			return;
		}

		//增加chkNum验证，必须填写正整数
		var patten = /^\d+$/;
		if (!patten.test(detailNum)) {
			api.alert({
				title: '提示',
				msg: '请填写正整数',
			}, function (ret, err) {

			});
			return;
		}

		//序列号
		var serialNo = $("#serialNo").val();

		if (serialNo == null || serialNo == "" || serialNo == undefined) {
			api.alert({
				title: '提示',
				msg: '序列号不能为空',
			}, function (ret, err) {

			});
			return;
		}
		var data = {
			values: {
				un: un,
				chkBillId: chkBillId,
				appVersion: api.appVersion,
				detailNum: detailNum,
				serialNo: serialNo,
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 30,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var mydata = eval("(" + data + ")");

				//成功
				if (mydata.code == '0') {
					retDetailId = mydata.retDetailId;
					getCitemsByDetailId(retDetailId);

				} else {
					api.toast({
						msg: mydata.message,
						duration: 3000
					});
				}
			}
		});

	}

	//查询检测单明细检测项
	function getCitemsByDetailId(detailId) {
		api.showProgress({
			style: 'default',
			animationType: 'fade',
			title: '努力加载中...',
			text: '获取检测项...',
			modal: true
		});

		var un = localStorage.getItem("un");
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getCitemsByDetailIdFHJ";

		var data = {
			values: {
				un: un,
				appVersion: api.appVersion,
				detailId: detailId,
			}
		}

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 30,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				api.hideProgress();
				var data = JSON.stringify(ret);
				var mydata = eval("(" + data + ")");
				if (mydata.code == '0') {
					myobj = eval(mydata.list);
					$("#citemForm").children().remove();
					for (var i = 0; i < myobj.length; i++) {
						var controlType = myobj[i].controlType; //获取控件类型（0手动输入1多选2单选）
						var citemId = myobj[i].citemId;
						var citemCode = myobj[i].citemCode;
						var citemName = myobj[i].citemName;
						var citemRemark = myobj[i].citemRemark;

						var labContent = "[" + (i + 1) + "][" + citemRemark + "]" + "[" + citemCode + "]" + "[" + citemName + "]";

						var itemId = "citem" + i;
						//alert(controlType);
						if ("0" == myobj[i].controlType) {
							var minValue = myobj[i].minValue;
							var maxValue = myobj[i].maxValue;
							var standardValue = myobj[i].standardValue;

							var valContent = "上限值：" + maxValue + " 下限值：" + minValue + " 标准值：" + standardValue;
							//手动输入
							// $("#citemForm").append("<div class='aui-input-row'><p style='width:" + api.frameWidth + "px ;text-align: left;color: #000;'>" + labContent +
							// 	"</p><div><input type='number' class='aui-input' placeholder='" + valContent + "' style='' id=\"" + itemId + "\" value='" + standardValue + "' onchange='setChkResult(\"" + minValue + "\",\"" + maxValue + "\",this.sysid)'/></div></div>");

							var itemValue = myobj[i].itemValue
							$("#citemForm").append("<div class='aui-input-row'><p style='width:" + api.frameWidth + "px ;text-align: left;color: #000;'>" + labContent +
								"</p><div><input type='number' class='aui-input' placeholder='" + valContent + "' style='' id=\"" + itemId + "\" value='" + itemValue + "' /></div></div>");
						} else if ("1" == myobj[i].controlType) {
							//多选（checkbox）
							var divStr = "<div class='aui-input-row'><p style='width:" + api.frameWidth + "px;text-align:left;color: #000;'>" + labContent + "</p><div class='aui-pull-left' >";
							var citemDs = eval(myobj[i].citemDs);
							for (var j = 0; j < citemDs.length; j++) {
								var citemDId = citemDs[j].sysid;
								var citemDCode = citemDs[j].citemDCode;
								var citemDName = citemDs[j].citemDName;
								// if (citemDName == "合格") {
								// 	divStr = divStr + "<input class='aui-checkbox' type='checkbox' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-checkbox-name'>" + citemDName + "</div>";
								// } else {
								divStr = divStr + "<input class='aui-checkbox' type='checkbox' name=\"" + itemId + "\" value=\"" + citemDId + "\" ><div class='aui-checkbox-name'>" + citemDName + "</div>";
								// }
							}

							$("#citemForm").append(divStr + "</div></div>");

						} else {
							//单选（radio）
							var divStr = "<div class='aui-input-row'><p style='width:" + api.frameWidth + "px; text-align:left;color: #000;'>" + labContent + "</p><div class='aui-pull-left' >";
							var citemDs = eval(myobj[i].citemDs);
							for (var j = 0; j < citemDs.length; j++) {
								var citemDId = citemDs[j].sysid;
								var citemDCode = citemDs[j].citemDCode;
								var citemDName = citemDs[j].citemDName;
								// if (citemDName == "合格") {
								// 	divStr = divStr + "<input class='aui-radio' type='radio' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-radio-name'>" + citemDName + "</div>";
								// } else {
								divStr = divStr + "<input class='aui-radio' type='radio' id=\"" + citemDId + "\" name=\"" + itemId + "\" value=\"" + citemDId + "\" onclick='radioClick(this,\"" + itemId + "\",\"" + citemDId + "\",\"" + citemDName +
									"\");'><div class='aui-radio-name'>" + citemDName + "</div>";
								// }
							}

							$("#citemForm").append(divStr + "</div></div>");
						}
					}
					var checkResultDivStr =
						"<div class='aui-input-row'><label class='aui-input-addon' style=''>检测结果</label><div class='aui-pull-right'><input id='radioBox1' class='aui-radio' type='radio' name='chkResult' value='7' onclick='chkResultOnClick(1)'><div class='aui-radio-name'>合格</div><input id='radioBox2' class='aui-radio' type='radio' name='chkResult' value='1' onclick='chkResultOnClick(2)'><div class='aui-radio-name'>不合格</div></div></div>"
					$("#citemForm").append(checkResultDivStr);
					$("#citemForm").append("<br /><div class='aui-content aui-content-padded' style='text-align:center;margin-bottom: 20px'><p><div class='aui-btn aui-btn-info aui-btn-block' onclick='doSave()' style='width: 100%;height: 100%;'>保存</div></p></div>");
					$("#citemForm").append(
						"<br /><div class='aui-content aui-content-padded' style='text-align:center;margin-bottom: 20px'><p><div class='aui-btn aui-btn-info aui-btn-block' onclick='dosaveBeforeConfirm()' style='width: 100%;height: 100%;'>最终质量判定</div></p></div>");
				} else {
					api.toast({
						msg: mydata.message,
						duration: 3000
					});
				}
			}
		});
	}

	function chkResultOnClick(type) {
		if (type == 1) {
			document.getElementById("radioBox1").style.backgroundColor = "green";
			document.getElementById("radioBox2").style.backgroundColor = "";
		} else {
			document.getElementById("radioBox1").style.backgroundColor = "";
			document.getElementById("radioBox2").style.backgroundColor = "red";
		}
	}

	//单选按钮值改变
	function radioClick(obj, itemId, citemDId, citemDName) {
		var group = document.getElementsByName(itemId);
		for (var i = 0; i < group.length; i++) {
			if (group[i].checked) {
				if (citemDName == '合格') {
					document.getElementById(citemDId).style.backgroundColor = "green";
				} else {
					document.getElementById(citemDId).style.backgroundColor = "red";
				}
			} else {
				document.getElementById(group[i].value).style.backgroundColor = "";
			}
		}
	}

	//最终质量判定
	function dosaveBeforeConfirm() {
		var chkResultCnt = 0;
		for (var i = 0; i < myobj.length; i++) {
			var controlType = myobj[i].controlType; //获取控件类型（0手动输入1多选2单选）
			var itemId = "citem" + i;
			var itemValue = "";
			var citemCode = myobj[i].citemCode;
			var citemName = myobj[i].citemName;
			if ("0" == myobj[i].controlType) {
				var minValue = myobj[i].minValue;
				var maxValue = myobj[i].maxValue;

				itemValue = document.getElementById(itemId).value;

				if (itemValue == null || itemValue == "" || itemValue == undefined) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测值不能为空!",
						duration: 5000
					});
					return;
				}

				if (parseFloat(itemValue) > parseFloat(maxValue) || parseFloat(itemValue) < parseFloat(minValue)) {
					chkResultCnt++;
				}
			} else if ("1" == myobj[i].controlType) {
				var checkBoxObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
				var cnt = 0;
				for (m = 0; m < checkBoxObj.length; m++) {
					if (checkBoxObj[m].checked) {
						itemValue = itemValue + checkBoxObj[m].value + ",";
						cnt++;
					}
				}
				if (checkBoxObj.length > 0) {
					//有选中，则最后一个需要去掉“,”
					itemValue = itemValue.substring(0, itemValue.length - 1);
				}

				if (cnt == 0) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测结果未判定!",
						duration: 5000
					});
					return;
				}

				//单选
			} else {
				var radioObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
				var cnt = 0;
				for (n = 0; n < radioObj.length; n++) {
					if (radioObj[n].checked) {
						itemValue = radioObj[n].value;
						cnt++;
					}
				}

				if (cnt == 0) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测结果未判定!",
						duration: 5000
					});
					return;
				}

				var citemDs = eval(myobj[i].citemDs);
				for (var j = 0; j < citemDs.length; j++) {
					var citemDId = citemDs[j].sysid;
					var citemDName = citemDs[j].citemDName;
					if (itemValue == citemDId) {
						if (citemDName == "不合格") {
							chkResultCnt++;
						}
					}
				}
			}
		}

		//不合格总数为0
		if (chkResultCnt == 0) {
			document.getElementById('radioBox1').checked = true;
			document.getElementById('radioBox2').checked = false;
			document.getElementById('radioBox1').style.backgroundColor = "green";
		} else {
			document.getElementById('radioBox1').checked = false;
			document.getElementById('radioBox2').checked = true;
			document.getElementById('radioBox2').style.backgroundColor = "red";
		}
	}

	function setChkResult(minValue, maxValue, id) {
		var itemValue = document.getElementById(id).value;
		//alert(itemValue);

		if (parseFloat(itemValue) < parseFloat(minValue) || parseFloat(itemValue) > parseFloat(maxValue)) {
			//alert("不合格");
			document.getElementById('radioBox2').checked = true;
		}


	}

	function citemObj(sysid, controlType, citemGid, citemCode, citemName, citemRemark, checkMode, specMax, maxValue, standardValue, minValue, specMin, chkValue, chkResult, ps, dutyId, sampleBatch) {
		this.sysid = sysid;
		this.controlType = controlType;
		this.citemGid = citemGid;
		this.citemCode = citemCode;
		this.citemName = citemName;
		this.citemRemark = citemRemark;
		this.checkMode = checkMode;
		this.specMax = specMax;
		this.maxValue = maxValue;
		this.standardValue = standardValue;
		this.minValue = minValue;
		this.specMin = specMin;
		this.chkValue = chkValue;
		this.chkResult = chkResult;
		this.ps = ps;
		this.dutyId = dutyId;
		this.sampleBatch = sampleBatch;
	}

	//保存
	function doSave() {
		var josnList = [];
		for (var i = 0; i < myobj.length; i++) {
			var controlType = myobj[i].controlType; //获取控件类型（0手动输入1多选2单选）
			var itemId = "citem" + i;
			var itemValue = "";
			var citemCode = myobj[i].citemCode;
			var citemName = myobj[i].citemName;
			if ("0" == myobj[i].controlType) {

				itemValue = document.getElementById(itemId).value;
				if (itemValue == null || itemValue == "" || itemValue == undefined) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测值不能为空!",
						duration: 5000
					});
					return;
				}

			} else if ("1" == myobj[i].controlType) {
				var cnt = 0;
				var checkBoxObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
				for (m = 0; m < checkBoxObj.length; m++) {
					if (checkBoxObj[m].checked) {
						itemValue = itemValue + checkBoxObj[m].value + ",";
						cnt++;
					}
				}
				if (checkBoxObj.length > 0) {
					//有选中，则最后一个需要去掉“,”
					itemValue = itemValue.substring(0, itemValue.length - 1);
				}

				if (cnt == 0) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测结果未判定!",
						duration: 5000
					});
					return;
				}
			} else {
				var cnt = 0;
				var radioObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
				for (n = 0; n < radioObj.length; n++) {
					if (radioObj[n].checked) {
						itemValue = radioObj[n].value;
						cnt++;
					}
				}

				if (cnt == 0) {
					api.toast({
						msg: "第" + (i + 1) + "行检测项【" + citemCode + "#" + citemName + "】检测结果未判定!",
						duration: 5000
					});
					return;
				}
			}

			//alert(myobj[i].sysid + ":" + itemValue);
			var sysid = myobj[i].sysid;
			var controlType = myobj[i].controlType;
			var citemGid = myobj[i].citemGid;
			var citemCode = myobj[i].citemCode;
			var citemName = myobj[i].citemName;
			var citemRemark = "";
			var checkMode = myobj[i].checkMode;
			var specMax = "";
			var maxValue = "";
			var standardValue = "";
			var minValue = "";
			var specMin = "";
			var chkValue = itemValue;
			var chkResult = "";
			var ps = "";
			var dutyId = "";
			var sampleBatch = "";
			var cObj = new citemObj(sysid, controlType, citemGid, citemCode, citemName, citemRemark, checkMode, specMax, maxValue, standardValue, minValue, specMin, chkValue, chkResult, ps, dutyId, sampleBatch);

			josnList.push(cObj);
		}

		//alert(JSON.stringify(josnList));

		var chkResultObj = document.getElementsByName("chkResult"); //这个是以标签的name来取控件
		var chkResultStr = ""; //检测结果（必填）
		for (j = 0; j < chkResultObj.length; j++) {
			if (chkResultObj[j].checked) {
				chkResultStr = chkResultObj[j].value;
			}
		}

		//alert(chkResultStr);

		if (chkResultStr == "") {
			api.alert({
				title: '提示',
				msg: '请选择检测结果',
			}, function (ret, err) {

			});
			return;
		}
		//保存填写的检测项检测值
		var un = localStorage.getItem('un');
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/saveUqcmChkBillDetailFHJ";

		var data = {
			values: {
				un: un,
				detailId: retDetailId,
				appVersion: api.appVersion,
				billId: chkBillId,
				chkResult: chkResultStr,
				detailNum: detailNum,
				rowData: JSON.stringify(josnList)
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 30,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var mydata = eval("(" + data + ")");
				if (mydata.code == '0') {

					api.toast({
						msg: "保存成功",
						duration: 3000
					});

					//保存成功，清空检测项的信息，需要重新点击“录入”查询检测项
					$("#citemForm").children().remove();
					myobj = [];
					retDetailId = "";

					$("#serialNo").val("");
				} else {
					api.toast({
						msg: mydata.message,
						duration: 3000
					});
				}
			}
		});
	}

	function closeWin() {
		api.closeWin();
	}
</script>

</html>